package com.ybwh.springboot2.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.ybwh.springboot2.dao.UserInfoMapper;
import com.ybwh.springboot2.model.po.UserInfo;
import com.ybwh.springboot2.service.UserInfoService;
import com.ybwh.springboot2.util.EntityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;

/**
 * <p>
 * 用户信息表服务接口实现类
 * </p>
 *
 * @author Fan Beibei
 * @version V1.0
 * @since 2022-12-23 12:04:14
 */
@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;


    @Override
    public int softDelByIds (Collection<Long> ids, Long userId) {
        Preconditions.checkArgument(null != ids && ids.size() > 0);
        Preconditions.checkArgument(null != userId);

        return userInfoMapper.softDelByPks(ids,userId,new Date());
    }

    @Override
    public UserInfo queryById (Long id) {
        Preconditions.checkArgument(null != id);

        return userInfoMapper.selectByPk(id,1L);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long addOrUpdate(UserInfo userInfo, Long userId) {
        Preconditions.checkArgument(null != userInfo);
        Preconditions.checkArgument(null != userId);

        if (null == userInfo.getId()) {
            EntityUtils.setPo5Field(userId, new Date(), userInfo);
            userInfoMapper.insert(userInfo);
        } else {
            EntityUtils.setPo2Field(userId, new Date(), userInfo);
            //这里不做删除
            userInfo.setDeleted(null);
            userInfoMapper.updateByPk(userInfo);
        }

        return userInfo.getId();

    }

    @Override
    public PageInfo<UserInfo> queryByPage(UserInfo param, int pageNum, int pageSize) {
        Preconditions.checkArgument(pageNum > 0);
        Preconditions.checkArgument(pageSize > 0);

        Page<UserInfo> page =  userInfoMapper.selectByPage(param,pageNum,pageSize);

        return new PageInfo<>(page);
    }






//------------以上是生成代码-----------------------------------------------------------------------------------------------------------------









}
